package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //先获得时间字符串
        List<LocalDate> dates=new ArrayList<>();
        dates.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dates.add(begin);
        }
        String join = StringUtils.join(dates, ",");

        //获得营业额字符串
        List<Double> turnoverList=new ArrayList<>();
        //根据时间查询营业额
        for(LocalDate date:dates){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map=new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status",5);
            Double amount=reportMapper.sumByMap(map);
            amount=amount==null?0.0:amount;
            turnoverList.add(amount);
        }
        return TurnoverReportVO.builder()
                .dateList(join)
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //先获得时间字符串
        List<LocalDate> dates=new ArrayList<>();
        dates.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dates.add(begin);
        }
        String join = StringUtils.join(dates, ",");
        //全部用户数量
        List<Integer> allUser=new ArrayList<>();
        //新增用户数量
        List<Integer> newUser=new ArrayList<>();
        for(LocalDate date:dates){
            LocalDateTime beginTime=LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            Integer allUserNum=userMapper.countByTime(null,endTime);
            Integer newUserNum=userMapper.countByTime(beginTime,endTime);
            allUser.add(allUserNum);
            newUser.add(newUserNum);
        }
        return UserReportVO.builder()
                .dateList(join)
                .newUserList(StringUtils.join(newUser,","))
                .totalUserList(StringUtils.join(allUser,","))
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //先获得时间字符串
        List<LocalDate> dates=new ArrayList<>();
        dates.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dates.add(begin);
        }
        String join = StringUtils.join(dates, ",");
        //每天订单数量
        List<Integer> totalOrder=new ArrayList<>();
        //有效订单数量
        List<Integer> validOrder=new ArrayList<>();
        for(LocalDate date:dates){
            LocalDateTime beginTime=LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);
            Integer totalOrderNum= orderMapper.count(beginTime,endTime,null);
            Integer validOrderNum= orderMapper.count(beginTime,endTime,5);
            totalOrder.add(totalOrderNum);
            validOrder.add(validOrderNum);
        }
        Integer total = totalOrder.stream().reduce(Integer::sum).get();
        Integer valid = validOrder.stream().reduce(Integer::sum).get();
        Double rate=0.0;
        if(total!=0){
            rate=valid*1.0/total;
        }
        return OrderReportVO.builder()
                .dateList(join)
                .orderCompletionRate(rate)
                .orderCountList(StringUtils.join(totalOrder,","))
                .totalOrderCount(total)
                .validOrderCount(valid)
                .validOrderCountList(StringUtils.join(validOrder,","))
                .build();
    }

    @Override
    public SalesTop10ReportVO salesTop10(LocalDate begin, LocalDate end) {
        //获得销量和姓名
        LocalDateTime beginTime=LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime=LocalDateTime.of(end,LocalTime.MAX);
        Map map=new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        List<GoodsSalesDTO> goodsSalesDTOList= orderMapper.getSalesTop10(map);

        List<String> name=goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> number=goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());

        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(name,","))
                .numberList(StringUtils.join(number,","))
                .build();
    }
}
